package com.aco.party.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;


public class LogAspect {
	private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
	
	public Object around(ProceedingJoinPoint pjp) throws Throwable {
		Long begain = System.currentTimeMillis();
//		logger.info("around start..");
		String methodName = pjp.getSignature().getDeclaringTypeName() + ", " + pjp.getSignature().getName();
//		logger.info("methodName:" + methodName);
		Object[] args = pjp.getArgs();
		if (args != null) {
			for (int i = 0; i < args.length; i++) {
				Object arg = args[i];
				if (arg instanceof HttpServletResponse) {
					HttpServletResponse response = (HttpServletResponse) args[i];
					response.setHeader("Pragma", "No-Cache");
					response.setHeader("Cache-Control", "no-cache, no-store");
					response.setDateHeader("Expires", 0);
					response.setHeader("Access-Control-Allow-Origin", "*");
					args[i] = response;
					break;
				}
			}
//			logger.info(methodName + "'s params:");
		}
		Object rs = null;
		try {
			rs = pjp.proceed();
			if (rs != null && String.valueOf(rs) != null) {
//				logger.info(methodName + "'s result:" + rs);
			}
			
		} catch (Exception ex) {
//			logger.info("error in around:" + new Date());
			ex.printStackTrace();
		}
		Long end = System.currentTimeMillis();
//		logger.info(String.format("around end..execute time is %s;",end - begain));
		return rs;
	}

	public void init(){
		System.out.println("初始化log aop");
	}
}
